package light.mvc.controller.hrs.front;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import light.mvc.controller.base.BaseController;
import light.mvc.framework.constant.GlobalConstant;
import light.mvc.pageModel.base.Json;
import light.mvc.pageModel.base.SessionInfo;
import light.mvc.pageModel.hrs.HRSData;
import light.mvc.service.hrs.HRSDataServiceI;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;


@Controller
@RequestMapping("/front/hrsdata")
public class HRSDataFrontController extends BaseController{

	@Autowired
	private HRSDataServiceI dataService;
	
	@RequestMapping("/add")
	@ResponseBody
	public Json add(HttpServletRequest request,HRSData data,@RequestParam("facePhoto") CommonsMultipartFile facePhoto,@RequestParam("tonguePhoto") CommonsMultipartFile tonguePhoto) {
		SessionInfo sessionInfo = (SessionInfo) request.getSession().getAttribute(GlobalConstant.SESSION_INFO_FRONT);	
		data.setUploaderType(sessionInfo.getRoleType());
		data.setUploaderId(sessionInfo.getId());
		data.setUploadTime(new Date());
		Json j = new Json(); 
//		 获取上传的图片的文件名
        String facePhotoFileName = facePhoto.getOriginalFilename();
        String tonguePhotoFileName = tonguePhoto.getOriginalFilename();
//      获取图片的扩展名
        String facePhotoExtensionName = facePhotoFileName.substring(facePhotoFileName.lastIndexOf(".") + 1);
        String tonguePhotoExtensionName = tonguePhotoFileName.substring(tonguePhotoFileName.lastIndexOf(".") + 1);
//      生成保存的图片文件名
        String facePhotoPrefixName = System.currentTimeMillis()+3+"";
		String facePhotoFilePath=GlobalConstant.uploadFolder+"/"+facePhotoPrefixName+"."+facePhotoExtensionName;//文件路径名
		String facePhotoRealPath = request.getSession().getServletContext().getRealPath("/")+facePhotoFilePath;
        File newFacePhotoFile=new File(facePhotoRealPath);
        String tonguePhotoPrefixName = System.currentTimeMillis()+"";
		String tonguePhotoFilePath=GlobalConstant.uploadFolder+"/"+tonguePhotoPrefixName+"."+tonguePhotoExtensionName;//文件路径名
		String tonguePhotoRealPath = request.getSession().getServletContext().getRealPath("/")+tonguePhotoFilePath;
        File newTonguePhotoFile=new File(tonguePhotoRealPath);
		try {
//          通过CommonsMultipartFile的方法直接写文件
			facePhoto.transferTo(newFacePhotoFile);
			tonguePhoto.transferTo(newTonguePhotoFile);
//			保存文件路径
	        data.setFacePhotoPath(facePhotoFilePath);
	        data.setTonguePhotoPath(tonguePhotoFilePath);
			dataService.add(data);
			j.setSuccess(true);
			j.setMsg("添加成功！");
		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		return j;
	}

}
